package com.servcice.impl;

import com.dao.ClazzDao;
import com.dto.BaseDto;
import com.dto.dataenum.MessageEnum;
import com.entity.Clazz;
import com.mapper.ClazzMapper;
import com.page.PageInfo;
import com.servcice.ClazzForStudentService;
import com.servcice.ClazzService;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ClazzServiceImpl implements ClazzService, ClazzForStudentService {
    @Resource
    ClazzDao clazzDao;


    @Override
    public BaseDto<Clazz> getAll() throws Exception {

        List<Clazz> clazzes = clazzDao.getAll();//
        int size = clazzes.size();
        MessageEnum messageEnum  = size>0?MessageEnum.SELECT_CLAZZALL_SUCCESS:MessageEnum.SELECT_CLAZZALL_FAIL;
        BaseDto dto = new BaseDto(messageEnum,clazzes);
        return dto;
    }

    public int addClazz(Clazz clazz) throws Exception {
        return clazzDao.addClazz(clazz);
    }

    public BaseDto<Clazz> getClazzsByPage(PageInfo pageInfo) throws Exception {
        BaseDto<Clazz> dto = new BaseDto<>(MessageEnum.SELECT_STUDENTBYPAGE_SUCCESS);
        dto.settList(clazzDao.getClazzsByPage(pageInfo));
        dto.setObj(pageInfo);
        return dto;
    }
    public BaseDto<Clazz> searchClazzByName(String name,PageInfo pageInfo) throws Exception{
        BaseDto<Clazz> dto = new BaseDto<>(MessageEnum.SELECT_STUDENTBYPAGE_SUCCESS);
        dto.settList(clazzDao.searchClazzByName(name,pageInfo));
        dto.setObj(pageInfo);
        return dto;
    }
    public BaseDto selectClazzWithScsAndCourse(String sno)throws Exception{
        BaseDto dto = new BaseDto<>(MessageEnum.SELECT_STUDENTBYSNOWITHSCS_SUCCESS);
        dto.setT(clazzDao.selectClazzWithScsAndCourse(sno));
        return dto;
    }

    public boolean studentChangeClazz(Clazz clazz,boolean isJoin){
       int countno = clazz.getCountno() + (isJoin?1:-1);
       clazz.setCountno(countno);
       int result = clazzDao.updateByPrimaryKeySelective(clazz);
       return result>0?true:false;
    }

}
